package com.tx.admin.modules.base.service.impl;

import org.apache.commons.lang3.StringUtils;

import com.tx.admin.core.service.impl.SuperServiceImpl;
import com.tx.admin.modules.base.entity.RbacEntity;
import com.tx.admin.core.exception.RbacException;

public abstract class RbacServiceImpl<E extends RbacEntity> extends SuperServiceImpl<E>  {

	private void filter(E entity) {
		E e = this.findById(entity.getId());
		boolean throwEx = false;
		if(!entity.getFilterUser().contains(e.getCrtUser())) {
			throwEx = true;
		}
		if(!entity.getFilterDepartment().contains(e.getCrtDepartment())) {
			throwEx = true;
		}
		
		if(throwEx) {
			RbacException.throwEx("你没有权限操作此数据");
			
		}
	
	}
	
	@Override
	public E save(E entity) {
		// TODO Auto-generated method stub
		if(StringUtils.isNotBlank(entity.getId())) {
			this.filter(entity);
		}

		return super.save(entity);
	}

	@Override
	public void delete(E entity) {
		// TODO Auto-generated method stub
		this.filter(entity);
		super.delete(entity);
	}

	
}
